package club.janna.concurrency.example.syncContainer;

import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.Vector;

@Slf4j
public class VectorExample3 {

    //java.util.ConcurrentModificationException
    private static void test1(Vector<Integer> v) {
        //foreach
        for(Integer i : v)
            if(i.equals(3))
                v.remove(i);
    }

    //java.util.ConcurrentModificationException
    private static void test2(Vector<Integer> v) {
        //iterator
        Iterator<Integer> iterator = v.iterator();
        while(iterator.hasNext()) {
            Integer i = iterator.next();
            if(i.equals(3))
                v.remove(i);
        }
    }

    private static void test3(Vector<Integer> v) {
        //for
        for(int i = 0; i < v.size(); i ++)
            if(i == 3)
                v.remove(i);
    }

    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
        for(int i = 0; i < 10; i ++)
            vector.add(i);
        test3(vector);
    }
}
